package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Role;
import com.example.demo.mapper.RoleMapper;
import com.example.demo.mapper.RoleMenuMapper;
import com.example.demo.service.RoleMenuService;
import com.example.demo.service.RoleService;
import com.example.demo.service.UserRoleService;
import com.example.demo.vo.RoleMenuRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;


/**
 * @author moguai
 * @version 1.0
 * @date 2022/3/18 16:05
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired(required = false)
    RoleMapper roleMapper;
    @Autowired(required = false)
    RoleMenuMapper roleMenuMapper;
    @Autowired
    UserRoleService userRoleService;
    @Autowired
    RoleMenuService roleMenuService;

    @Override
    public void insertRole(RoleMenuRequest request) {
        Role temp = new Role(request.getRoleId(), request.getRoleName(), System.currentTimeMillis());
        this.save(temp);
        List<Integer> menuList = request.getMenuList();
        roleMenuMapper.insertMenuList(request.getRoleId(), menuList);
    }

    @Override
    public void updateRole(RoleMenuRequest request) {
        Role temp = new Role(request.getRoleId(), request.getRoleName(), System.currentTimeMillis());
        this.updateById(temp);
        List<Integer> menuList = request.getMenuList();
        roleMenuMapper.deleteListByTableName("user_role", Collections.singletonList(request.getRoleId()));
        roleMenuMapper.insertMenuList(request.getRoleId(), menuList);

    }

    @Override
    public void deleteRole(List<Integer> ids) {
        roleMapper.deleteBatchIds(ids);
        userRoleService.deleteByIds("role_id", ids);
        roleMenuService.deleteRoleMenu("role_id", ids);
    }


    @Override
    public Role selectRole(Integer id) {
        Role role = roleMapper.selectById(id);
        return role;
    }

    @Override
    public List<Role> SelectRoleByUser(List<Integer> RolesId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in("role_id", RolesId);
        List<Role> list = roleMapper.selectList(wrapper);
        return list;
    }

}
